<?

include "Facture_fournisseur.php";
include "Line_fournisseur.php";
include "Paiement_fournisseur.php";
include "Stats.php";

class AchatsController extends Controller {
    
		public $js = array("achats",
		"jQuery/plugins/jqPlot/jquery.jqplot.min",
		"jQuery/plugins/jqPlot/jqplot.canvasAxisLabelRenderer.min",
	
		"jQuery/plugins/jqPlot/plugins/jqplot.canvasTextRenderer.min",
		"jQuery/plugins/jqPlot/plugins/jqplot.canvasAxisLabelRenderer.min",
		"jQuery/plugins/jqPlot/plugins/jqplot.dateAxisRenderer.min",
		"jQuery/plugins/jqPlot/plugins/jqplot.canvasAxisTickRenderer.min",
		"jQuery/plugins/jqPlot/plugins/jqplot.categoryAxisRenderer.min",
		"jQuery/plugins/jqPlot/plugins/jqplot.barRenderer.min",
		"jQuery/plugins/jqPlot/plugins/jqplot.highlighter.min",
		"jQuery/plugins/jqPlot/plugins/jqplot.cursor.min",
		"jQuery/plugins/jqPlot/plugins/jqplot.dateAxisRenderer.min"
	);
	
	public $css = array("achats","jQuery/jqPlot/jquery.jqplot.min");
	
	public function mainAction(){
		
		$connect = $this->connections;
		
		$aContacts = $connect->fetchAllModels("Fournisseur",array("user_id"=>$_SESSION["user_id"]));
		$factures = $connect->fetchAllModels("Facture_fournisseur",array("user_id"=>$_SESSION["user_id"]),array("date"=>"DESC"));
		$typesReglements =  $connect->fetchAllModels("Type_reglement");
		$aTVA =  $connect->fetchAllModels("Tva");
		$planComptable =  $connect->fetchAllModels("Plan_comptable",array("code"=>array("LIKE"=>"6%")));
		$paiements =  $connect->fetchAllModels("Paiement_fournisseur",array(),array("paiement_fournisseur.date"=>"DESC"),
			"facture_fournisseur INNER JOIN paiement_fournisseur ON paiement_fournisseur._facture = facture_fournisseur._id");

		foreach($paiements as $paiement){
			$aPaiementsFournisseur[$paiement->_facture] = $factures[$paiement->_facture];
		}
		
		$mysql = $connect->getConnection();
		$stats = new Stats($mysql);
		$achats = $stats->achats();
		
		$vars = array(
			"factures"=>$factures,
			"aContacts"=>$aContacts,
			"typesReglements"=>$typesReglements,
			"aTVA"=>$aTVA,
			"planComptable"=>$planComptable,
			"aPaiementsFournisseur"=>$aPaiementsFournisseur,
			"achats"=>$achats
		);
		return $this->render("achats/main",$vars);
	}
	public function getFactureFournisseurAction($facture_id){
		$connect = $this->connections;
		$facture = $connect->fetchModels("Facture_fournisseur",array("_id"=>$facture_id));
		$lines = $connect->fetchAllModels("Line_fournisseur",array("_facture"=>$facture_id));
		$facture->lines = $lines;		
		return json_encode($facture);
	}
	public function newFactureFournisseurAction(){
		$connect = $this->connections;
		
		if(isset($_POST["facture_id"]) && $_POST["facture_id"] != ""){
			$connect->remove("Facture_fournisseur",array("_id"=>$_POST["facture_id"]));
			$connect->remove("Line_fournisseur",array("_facture"=>$_POST["facture_id"]));
			$_POST["_id"] = $_POST["facture_id"];
		}
		
		/* Enregistrement de la facture */
		$facture = new Facture_fournisseur();
		$_POST["date"] = date("Y-m-d",strtotime($_POST["date"])).date(" H:i:s");
		$_POST["echeance"] = date("Y-m-d H:i:s",strtotime($_POST["echeance"]));
		$facture->fromArray($_POST);
		$facture->user_id = $_SESSION["user_id"];
		$facture->_id = $connect->save($facture);
		
		/* Enregistrement des lignes */
		foreach($_POST["lines"] as $line){
			if($line["qte"] != ""){
				$lineFacture = new Line_fournisseur();
				$lineFacture->qte = $line["qte"];
				$lineFacture->description = $line["description"];
				$lineFacture->montant_ttc = $line["montant_ttc"];
				$lineFacture->classe = $line["plan"];
				$lineFacture->taux_tva = $line["taux_tva"];
				$lineFacture->_facture = $facture->_id;
				$connect->save($lineFacture);
			}
		}
		return json_encode(array("response_code"=>"ok"));
	}
	function removeFactureFournisseurAction($facture_id){
		$connect = $this->connections;
		
		$connect->remove("Facture_fournisseur",array("_id"=>$facture_id));
		$connect->remove("Line_fournisseur",array("_facture"=>$facture_id));
		
		return json_encode(array("response_code"=>"ok"));
	}
	public function enregistrerPaiementFournisseurAction($facture_id){
		$connect = $this->connections;
		
		$paiement_fournisseur = new Paiement_fournisseur();
		$paiement_fournisseur->_facture = $facture_id;
		$paiement_fournisseur->date = date("Y-m-d H:i:s");
		
		$connect->save($paiement_fournisseur);
		
		return json_encode(array("response_code"=>"ok")); 
		
	}
	public function removePaiementFournisseurAction($facture_id){
		$connect = $this->connections;
		$connect->remove("Paiement_fournisseur",array("_facture"=>$facture_id));
		return json_encode(array("response_code"=>"ok"));
	}
	public function previewFactureAction($facture_id){
		$connect = $this->connections;
		
		$facture = $connect->fetchModels("Facture_fournisseur",array("_id"=>$facture_id));
		$lines = $connect->fetchAllModels("Line_fournisseur",array("_facture"=>$facture_id));
		$contact = $connect->fetchModels("Fournisseur",array("_id"=>$facture->_fournisseur));
		return $this->render("preview/facture_fournisseur", array("facture"=>$facture,"contact"=>$contact,"lines"=>$lines));
	}
}
?>